/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.dal;

import edu.fpt.hib.Apartment;
import edu.fpt.hib.HibernateUtil;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Admin
 */
public class ApartmentDAL {
    public static void insert(Apartment entity){
        Session session = null;
        Transaction tran = null;
        try{
          session = HibernateUtil.getSessionFactory().openSession();
          tran = session.beginTransaction();
          session.save(entity);
          tran.commit();
        }
        finally{
            session.close();
        }
    }
    public static void update(Apartment entity){
        Session session = null;
        Transaction tran = null;
        try{
          session = HibernateUtil.getSessionFactory().openSession();
          tran = session.beginTransaction();
          session.update(entity);
          tran.commit();
        }
        finally{
            session.close();
        }
    }
    
    public static Apartment getApartmentByUserHQL(String name){
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Apartment> list = new ArrayList<Apartment>();
            Query query = session.createQuery("from Apartment as apart where apart.users.userId='"+name+"'");
            Apartment apart = (Apartment) query.uniqueResult();
            tran.commit();
            return apart;
        } finally {
            session.close();
        }
    }
    
    public static Apartment getApartmentByID(String name){
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Apartment> list = new ArrayList<Apartment>();
            Query query = session.createQuery("from Apartment where apartid='"+name+"'");
            Apartment apart = (Apartment) query.uniqueResult();
            tran.commit();
            return apart;
        } finally {
            session.close();
        }
    }
    
    public static ArrayList<String> getListApartID(){
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Apartment> list = new ArrayList<Apartment>();
            ArrayList<String> listID = new ArrayList<String>();
            Query query = session.createQuery("from Apartment where apartStatus = 'Avai'");
            list = (ArrayList<Apartment>) query.list();
            for(int i=0;i<list.size();i++){
                listID.add(list.get(i).getApartId()); 
            }
            tran.commit();
            return listID;
        } finally {
            session.close();
        }
    }
    
//    public static String getApartmentByUser(String name) throws ClassNotFoundException, SQLException {
//        Connection con = null;
//        PreparedStatement pstmt = null;
//        String sql = "select * from Apartment where userID='" + name + "'";
//        try {
//            con = ConnectionUtil.openConnection();
//            pstmt = con.prepareStatement(sql);
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                return rs.getString("apartID");
//            }
//            return "None";
//        } finally {
//            try {
//                pstmt.close();
//                con.close();
//            } catch (Exception e) {
//            }
//        }
//    }
//    
//     public static ApartmentEntity getApartmentEntity(String apartID) throws ClassNotFoundException, SQLException, ParseException {
//        Connection con = null;
//        PreparedStatement pstmt = null;
//        String sql = "select * from apartment where apartID='" + apartID + "'";
//        try {
//            con = ConnectionUtil.openConnection();
//            pstmt = con.prepareStatement(sql);
//            ApartmentEntity apart = new ApartmentEntity();
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                apart.setApartID(rs.getString("apartID"));
//                apart.setUserID(rs.getString("userID"));
//                apart.setApartStatus(rs.getString("apartStatus"));
//                apart.setIsOwn(rs.getBoolean("isOwn"));
//                apart.setUsingServices(rs.getString("usingServices"));
//                apart.setBillStatus(rs.getBoolean("billStatus"));
//                apart.setElectricNum(Integer.parseInt(rs.getString("electricNum")));
//                apart.setWaterNum(Integer.parseInt(rs.getString("waterNum")));
//            }
//            return apart;
//        } finally {
//            try {
//                pstmt.close();
//                con.close();
//            } catch (Exception e) {
//            }
//        }
//    }
     public static ArrayList<Apartment> listAllUsing() {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Apartment> list = new ArrayList<Apartment>();
            Query query = session.createQuery("from Apartment where apartStatus = 'Unavai'");
            list = (ArrayList<Apartment>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }
     
     public static ArrayList<Apartment> listAll() {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Apartment> list = new ArrayList<Apartment>();
            Query query = session.createQuery("from Apartment");
            list = (ArrayList<Apartment>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }
}
